package com.hongpu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongpu.config.SecurityContextUtil;
import com.hongpu.domain.MultiTenant;
import com.hongpu.dto.MultiTenantDto;
import com.hongpu.mapper.MultiTenantMapper;
import com.hongpu.service.MultiTenantService;
import com.hongpu.service.SysUserService;
import com.hongpu.utils.ResultDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Michael zhang
 * @description 针对表【multi_tenant(多租户管理)】的数据库操作Service实现
 * @createDate 2025-05-30 09:24:21
 */
@Service
public class MultiTenantServiceImpl extends ServiceImpl<MultiTenantMapper, MultiTenant>
        implements MultiTenantService {
    @Autowired
    MultiTenantMapper baseMapper;
    @Autowired
    SysUserService sysUserService;


    /**
     * 新增
     *
     * @param multiTenant
     */
    @Override
    public ResultDto add(MultiTenant multiTenant) {
        try {
            String slug = multiTenant.getSlug().toUpperCase();
            //验证slug唯一
            MultiTenant tenant = baseMapper.selectOne(new QueryWrapper<MultiTenant>().eq("slug", slug));
            if (tenant != null) {
                return ResultDto.fail("租户唯一识别代码已存在，请重新设置");
            }
            multiTenant.setSlug(slug);
            return save(multiTenant) ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }


    /**
     * 更新多租户信息
     *
     * @param multiTenant
     * @return
     */
    @Override
    public boolean updatedById(MultiTenant multiTenant) {
        String slug = multiTenant.getSlug().toUpperCase();
        multiTenant.setSlug(slug);
        multiTenant.setUpdateBy(SecurityContextUtil.getUserId());
        return baseMapper.updatedById(multiTenant);
    }

    /**
     * 将租户状态设置为删除状态
     *
     * @param tenantId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDto updateStatusDeleted(Long tenantId) {
        try {
            Long userId = SecurityContextUtil.getUserId();
            baseMapper.updateStatusDeleted(tenantId, userId);
            sysUserService.ignoreTenantIdAndUpdateStatusDeleted(tenantId);
            return ResultDto.ok();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 将租户状态设置为暂停状态
     *
     * @param tenantId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDto updateStatusSuspended(Long tenantId) {
        try {
            Long userId = SecurityContextUtil.getUserId();
            baseMapper.updateStatusSuspended(tenantId, userId);
            sysUserService.ignoreTenantIdAndUpdateStatusDeleted(tenantId);
            return ResultDto.ok();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 将租户状态设置为启用状态
     *
     * @param tenantId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDto updateStatusActive(Long tenantId) {
        try {
            Long userId = SecurityContextUtil.getUserId();
            baseMapper.updateStatusActive(tenantId, userId);
            sysUserService.ignoreTenantIdAndUpdateStatusActive(tenantId);
            return ResultDto.ok();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 分页
     *
     * @param multiTenantDto
     */
    @Override
    public IPage<MultiTenant> selectPage(MultiTenantDto multiTenantDto) {
        IPage page = new Page(multiTenantDto.getCurrent(), multiTenantDto.getSize());
       try {
           return baseMapper.selectPage(page, multiTenantDto);
       } catch (Exception e) {
           e.printStackTrace();
           return page;
       }
    }

    @Override
    public List<MultiTenant> getAllTenant() {
        return baseMapper.getAllTenant();
    }

    /**
     * 租户详情
     *
     * @param tenantId
     */
    @Override
    public MultiTenant detail(Long tenantId) {
        return baseMapper.detail(tenantId);
    }

    /**
     * 根据租户ID查询用于URL或系统识别的唯一代码
     *
     * @param tenantId
     */
    @Override
    public String getSlugByTenantId(Long tenantId) {
        return baseMapper.getSlugByTenantId(tenantId);
    }
}




